<script setup>

import {ref} from "vue";
import request from '../../utils/request';
import { ElMessage, ElMessageBox } from 'element-plus'
// 定义响应式数据
const user = ref(JSON.parse(localStorage.getItem('happyuser') || '{}'));

function handleAvatarSuccess(response) {
  user.value.avatar = response.data;
}

const save = () => {
  request({
    url:  '/users/update' ,
    method: 'put' , // 根据id是否存在来判断是put还是post
    data: user.value
   }).then((res) => {
    if(res.data.code == 200){
      ElMessage({
      message: '保存成功',
      type: 'success',
     })
     localStorage.setItem('happyuser',JSON.stringify(user.value))
     // 刷新页面
     window.location.reload();
    }else{
      ElMessage.error(res.data.msg)
    } 
   })
}

</script>
<template>
 <div class="main">
  <el-card  class="elcard">
      <el-form :model="user" label-width="100px" style="padding-right: 50px">
        <div style="margin: 15px; text-align: center">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8083/files/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              style="margin-left: 30px; "
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" style="box-shadow: 10px 10px 0px 10px rgb(98, 101, 101);"></i>
          </el-upload>
        </div>
        <el-form-item  prop="username" class="in">
          <span style="font-size: 15px; font-weight: 1000;"><el-icon><UserFilled /></el-icon> 用户名：</span>
          <el-input v-model="user.username" placeholder="用户名" disabled ></el-input>
        </el-form-item>
        <el-form-item prop="name" class="in">
          <span style="font-size: 15px; font-weight: 1000;"><el-icon><UserFilled /></el-icon> 姓名：</span>
          <el-input v-model="user.name" placeholder="姓名" ></el-input>
        </el-form-item>
        <el-form-item  prop="phone" class="in">
          <span style="font-size: 15px; font-weight: 1000;"><el-icon><UserFilled /></el-icon> 电话：</span>
          <el-input v-model="user.phone" placeholder="电话" ></el-input>
        </el-form-item>
        <el-form-item  prop="email" class="in">
          <span ><el-icon><UserFilled /></el-icon> 邮箱：</span>
          <el-input v-model="user.email" placeholder="邮箱"  style=""></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="save">保 存</el-button>
        </div>
      </el-form>
    </el-card>

 </div>
    
</template>
<style scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.in {
  display: flex;
  width: 60%;
  margin:10px auto;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  border-radius: 50%;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
  border-radius: 50%;
  box-shadow: 10px 0 10px rgb(98, 101, 101);
}
.main{
  width: 100%;
  height: 100%;
  margin: 0px auto;
  padding-top: 100px;
  background-image: url(../assets/pbeijing.png);
   background-size: cover;

}

.elcard{
  width: 55%;
  /* height: ; */
  margin: 0px auto;
  border-radius: 20px;
  /* background-color: rgb(246, 246, 246); */
  background-color: transparent;
}
</style>